﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Hirham.OpenMI.Sdk.Backbone;

using Hirham.OpenMI.Engine;


namespace HirHam.OpenMI.SDK.UnitTest
{
  [TestClass]
  public class CoupledRun
  {
    [TestMethod]
    public void CoupledWithKarup()
    {

      File.Copy(@"..\..\..\TestData\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\hirham_data_ready.txt", true);
      File.Copy(@"..\..\..\TestData\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\hirham_data_ready_timestep.txt", true);

      Hirham.OpenMI.Gui.Core.UIModel hirham = new Hirham.OpenMI.Gui.Core.UIModel();
      hirham.ReadOMIFile(@"..\..\..\TestData", "hirham_karup-test.omi");

      Hirham.OpenMI.Gui.Core.UIModel karup = new Hirham.OpenMI.Gui.Core.UIModel();
      karup.ReadOMIFile(@"..\..\..\TestData", "Karup_SWAT_innergrid.omi");

      for (int i = 0; i < karup.LinkableComponent.InputExchangeItemCount; i++)
      {
        var org = karup.LinkableComponent.GetInputExchangeItem(i);
      }


      Link l = new Link(hirham.LinkableComponent,
        hirham.LinkableComponent.GetOutputExchangeItem(4).ElementSet,
        hirham.LinkableComponent.GetOutputExchangeItem(4).Quantity,
        karup.LinkableComponent,
        karup.LinkableComponent.GetInputExchangeItem(23).ElementSet,
        karup.LinkableComponent.GetInputExchangeItem(23).Quantity,
        "desc",
        "1",
        new System.Collections.ArrayList() { hirham.LinkableComponent.GetOutputExchangeItem(4).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l);
      karup.LinkableComponent.AddLink(l);


      Link l2 = new Link(karup.LinkableComponent,
  karup.LinkableComponent.GetOutputExchangeItem(12).ElementSet,
  karup.LinkableComponent.GetOutputExchangeItem(12).Quantity,
  hirham.LinkableComponent,
  hirham.LinkableComponent.GetInputExchangeItem(0).ElementSet,
  hirham.LinkableComponent.GetInputExchangeItem(0).Quantity,
  "desc",
  "2",
  new System.Collections.ArrayList() { karup.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l2);
      karup.LinkableComponent.AddLink(l2);


      Link l3 = new Link(karup.LinkableComponent,
                karup.LinkableComponent.GetOutputExchangeItem(15).ElementSet,
                karup.LinkableComponent.GetOutputExchangeItem(15).Quantity,
                hirham.LinkableComponent,
                hirham.LinkableComponent.GetInputExchangeItem(1).ElementSet,
                hirham.LinkableComponent.GetInputExchangeItem(1).Quantity,
                "desc",
                "3",
                new System.Collections.ArrayList() { karup.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });


      hirham.LinkableComponent.AddLink(l3);
      karup.LinkableComponent.AddLink(l3);

      hirham.LinkableComponent.Validate();
      hirham.LinkableComponent.Prepare();

      karup.LinkableComponent.Validate();
      karup.LinkableComponent.Prepare();




      hirham.LinkableComponent.GetValues(hirham.LinkableComponent.TimeHorizon.End, l.ID);




      karup.LinkableComponent.Dispose();
      hirham.LinkableComponent.Dispose();

      FileInfo f = new FileInfo(@"..\..\..\TestData\ahfl.in");
      Assert.IsTrue(DateTime.Now.Subtract(f.LastWriteTime).TotalSeconds < 5);

      FileInfo f2 = new FileInfo(@"..\..\..\TestData\tsl.in");
      Assert.IsTrue(DateTime.Now.Subtract(f2.LastWriteTime).TotalSeconds < 5);


      Assert.IsTrue(FileComparer(f.FullName, @"..\..\..\TestData\ahfl-unittest.in"));
      Assert.IsTrue(FileComparer(f2.FullName, @"..\..\..\TestData\tsl-unittest.in"));
    }


    [TestMethod]
    public void CoupledWithSimpleModel()
    {
      File.Copy(@"..\..\..\TestData\simpletest\hirham - Kopi.out", @"..\..\..\TestData\simpletest\hirham.out", true);

      File.Copy(@"..\..\..\TestData\simpletest\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\simpletest\hirham_data_ready.txt", true);
      File.Copy(@"..\..\..\TestData\simpletest\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\simpletest\hirham_data_ready_timestep.txt", true);

      Hirham.OpenMI.Gui.Core.UIModel hirham = new Hirham.OpenMI.Gui.Core.UIModel();
      hirham.ReadOMIFile(@"..\..\..\TestData\simpletest", "2x2_hirham.omi");

      Hirham.OpenMI.Gui.Core.UIModel mshe = new Hirham.OpenMI.Gui.Core.UIModel();
      mshe.ReadOMIFile(@"..\..\..\TestData\simpletest", "MIKESHE_WM_simple_test_model.omi");

      for (int i = 0; i < mshe.LinkableComponent.InputExchangeItemCount; i++)
      {
        var org = mshe.LinkableComponent.GetInputExchangeItem(i);
      }


      Link l = new Link(hirham.LinkableComponent,
        hirham.LinkableComponent.GetOutputExchangeItem(5).ElementSet,
        hirham.LinkableComponent.GetOutputExchangeItem(5).Quantity,
        mshe.LinkableComponent,
        mshe.LinkableComponent.GetInputExchangeItem(0).ElementSet,
        mshe.LinkableComponent.GetInputExchangeItem(0).Quantity,
        "desc",
        "1",
        new System.Collections.ArrayList() { hirham.LinkableComponent.GetOutputExchangeItem(4).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l);
      mshe.LinkableComponent.AddLink(l);


      Link l2 = new Link(mshe.LinkableComponent,
mshe.LinkableComponent.GetOutputExchangeItem(12).ElementSet,
mshe.LinkableComponent.GetOutputExchangeItem(12).Quantity,
hirham.LinkableComponent,
hirham.LinkableComponent.GetInputExchangeItem(0).ElementSet,
hirham.LinkableComponent.GetInputExchangeItem(0).Quantity,
"desc",
"2",
new System.Collections.ArrayList() { mshe.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l2);
      mshe.LinkableComponent.AddLink(l2);


      Link l3 = new Link(mshe.LinkableComponent,
                mshe.LinkableComponent.GetOutputExchangeItem(15).ElementSet,
                mshe.LinkableComponent.GetOutputExchangeItem(15).Quantity,
                hirham.LinkableComponent,
                hirham.LinkableComponent.GetInputExchangeItem(1).ElementSet,
                hirham.LinkableComponent.GetInputExchangeItem(1).Quantity,
                "desc",
                "3",
                new System.Collections.ArrayList() { mshe.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });


      hirham.LinkableComponent.AddLink(l3);
      mshe.LinkableComponent.AddLink(l3);


      hirham.LinkableComponent.Validate();
      hirham.LinkableComponent.Prepare();

      mshe.LinkableComponent.Validate();
      mshe.LinkableComponent.Prepare();




      hirham.LinkableComponent.GetValues(hirham.LinkableComponent.TimeHorizon.End, l.ID);




      mshe.LinkableComponent.Dispose();
      hirham.LinkableComponent.Dispose();


    }

    [TestMethod]
    public void SimpleTestWithErrors()
    {
      File.Copy(@"..\..\..\TestData\simpletest\hirham_witherror.out", @"..\..\..\TestData\simpletest\hirham.out", true);

      File.Copy(@"..\..\..\TestData\simpletest\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\simpletest\hirham_data_ready.txt", true);
      File.Copy(@"..\..\..\TestData\simpletest\hirham_data_ready - Kopi.txt", @"..\..\..\TestData\simpletest\hirham_data_ready_timestep.txt", true);

      Hirham.OpenMI.Gui.Core.UIModel hirham = new Hirham.OpenMI.Gui.Core.UIModel();
      hirham.ReadOMIFile(@"..\..\..\TestData\simpletest", "2x2_hirham.omi");

      Hirham.OpenMI.Gui.Core.UIModel mshe = new Hirham.OpenMI.Gui.Core.UIModel();
      mshe.ReadOMIFile(@"..\..\..\TestData\simpletest", "MIKESHE_WM_simple_test_model.omi");

      for (int i = 0; i < mshe.LinkableComponent.InputExchangeItemCount; i++)
      {
        var org = mshe.LinkableComponent.GetInputExchangeItem(i);
      }


      Link l = new Link(hirham.LinkableComponent,
        hirham.LinkableComponent.GetOutputExchangeItem(5).ElementSet,
        hirham.LinkableComponent.GetOutputExchangeItem(5).Quantity,
        mshe.LinkableComponent,
        mshe.LinkableComponent.GetInputExchangeItem(0).ElementSet,
        mshe.LinkableComponent.GetInputExchangeItem(0).Quantity,
        "desc",
        "1",
        new System.Collections.ArrayList() { hirham.LinkableComponent.GetOutputExchangeItem(4).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l);
      mshe.LinkableComponent.AddLink(l);


      Link l2 = new Link(mshe.LinkableComponent,
mshe.LinkableComponent.GetOutputExchangeItem(12).ElementSet,
mshe.LinkableComponent.GetOutputExchangeItem(12).Quantity,
hirham.LinkableComponent,
hirham.LinkableComponent.GetInputExchangeItem(0).ElementSet,
hirham.LinkableComponent.GetInputExchangeItem(0).Quantity,
"desc",
"2",
new System.Collections.ArrayList() { mshe.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });

      hirham.LinkableComponent.AddLink(l2);
      mshe.LinkableComponent.AddLink(l2);


      Link l3 = new Link(mshe.LinkableComponent,
                mshe.LinkableComponent.GetOutputExchangeItem(15).ElementSet,
                mshe.LinkableComponent.GetOutputExchangeItem(15).Quantity,
                hirham.LinkableComponent,
                hirham.LinkableComponent.GetInputExchangeItem(1).ElementSet,
                hirham.LinkableComponent.GetInputExchangeItem(1).Quantity,
                "desc",
                "3",
                new System.Collections.ArrayList() { mshe.LinkableComponent.GetOutputExchangeItem(12).GetDataOperation(6) });


      hirham.LinkableComponent.AddLink(l3);
      mshe.LinkableComponent.AddLink(l3);


      hirham.LinkableComponent.Validate();
      hirham.LinkableComponent.Prepare();

      mshe.LinkableComponent.Validate();
      mshe.LinkableComponent.Prepare();




      hirham.LinkableComponent.GetValues(hirham.LinkableComponent.TimeHorizon.End, l.ID);




      mshe.LinkableComponent.Dispose();
      hirham.LinkableComponent.Dispose();


    }





    public bool FileComparer(string file1, string file2)
    {
      string file_content1;
      using (StreamReader sr = new StreamReader(file1))
      {
        file_content1 = sr.ReadToEnd();
      }

      string file_content2;
      using (StreamReader sr = new StreamReader(file2))
      {
        file_content2 = sr.ReadToEnd();
      }

      return file_content1 == file_content2;

    }

  }
}
